package com.heima.estatemanagement.controller;
import com.github.pagehelper.Page;
import com.heima.estatemanagement.common.MessageConstant;
import com.heima.estatemanagement.common.PageResult;
import com.heima.estatemanagement.common.Result;
import com.heima.estatemanagement.common.StatusCode;
import com.heima.estatemanagement.domain.TbHouse;
import com.heima.estatemanagement.mapper.CommunityMapper;
import com.heima.estatemanagement.service.TbHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.sound.midi.Track;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/homelist")
public class HomeListController {

    @Autowired
    TbHouseService tbHouseService;
    @RequestMapping("/find")
    public Result findAll(){
        List<TbHouse> all =tbHouseService.findAll();
        return new Result(false,0,"请求成功",all);
    }

    @RequestMapping("/search")
    public PageResult search( @RequestBody Map SearchMap){
        Page<TbHouse> search = tbHouseService.search(SearchMap);
        System.out.println(search.getTotal());
        return new PageResult(true,StatusCode.OkLAYUI,MessageConstant.COMMUNITY_SEARCH_SUCCESS,search.getResult(),search.getTotal());
    }

    @RequestMapping("/finByCommunityId")
    public Result findByNameandById(int community_id){
        TbHouse byNameAndById = tbHouseService.findByNameAndById( community_id);
        System.out.println("byNameAndById================>"+byNameAndById);
        return new Result(true, StatusCode.OK, MessageConstant.COMMUNITY_SEARCH_SUCCESS,byNameAndById);
    }

    @RequestMapping("/del")
    public Result delById(@RequestBody List<Integer> ids){
        boolean b = tbHouseService.delByIds(ids);
        return new Result(true,StatusCode.OK,MessageConstant.HO_HOUSE_DELETE_SUCCESS);
    }
    @RequestMapping("delById")
    public Result delById(int id){
        if (tbHouseService.delById(id)){
            Boolean flag = tbHouseService.delById(id);
            return new  Result(true,StatusCode.OK,MessageConstant.HO_HOUSE_DELETE_SUCCESS);
        }else {
            return new  Result(true,StatusCode.ERROR,MessageConstant.HO_HOUSE_DELETE_ERROR);
        }
    }
    @RequestMapping("add")
    public Result HouseAdd(TbHouse tbHouse){
        tbHouseService.add(tbHouse);

        return null;
    }

}
